/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.sheet.nested;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellRenderer;

import twaver.Element;
import twaver.ElementAttribute;
import twaver.Node;
import twaver.TPropertyDescriptor;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.table.Category;
import twaver.table.PropertySheetEvent;
import twaver.table.PropertySheetListener;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import demo.DemoNode;
import demo.DemoPane;
import demo.MainPane;

public class NestedCategoryDemo extends DemoPane {

    private Node node = new Node();
    private Color alternateColor = new Color(238, 238, 238);
    private JCheckBox alternate = new JCheckBox("Alternate", false);
    
    private TPropertySheet sheet = new TPropertySheet(){
		public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
			Component component = super.prepareRenderer(renderer, row, col);
			if(alternate.isSelected() && !this.isRowSelected(row)){
	            if (row % 2 == 0) {
	            	component.setBackground(alternateColor);
	            } else {
	            	component.setBackground(Color.WHITE);
	            }
	            ((JComponent)component).setOpaque(true);
			}
			return component;
		}
    };
	
	public NestedCategoryDemo(){ 
        List attributes = new ArrayList();       
        Iterator it = MainPane.box.iteratorReverse();
        while(it.hasNext()){
        	Element element = (Element)it.next();
        	String name = element.getName();
        	
        	if(element instanceof DemoNode){
        		Object value = ((DemoNode)element).getDemoClass().getName();
        		ElementAttribute attribute = new ElementAttribute();
        		attribute.setClientPropertyKey(name);
        		attribute.setDisplayName(name);
        		attribute.setDescription("Demo with name '" + name + "'from " + value + ".");
        		attribute.setIcon(element.getIcon());
        		List categoryNames = new ArrayList();
            	Element parent = element.getParent();
            	while(parent != null){
            		categoryNames.add(0, parent.getName());
            		parent = parent.getParent();
            	}
            	attribute.setCategoryNames(categoryNames);
            	attribute.addParam("element", element);
        		attributes.add(attribute);
        		node.putClientProperty(name, value);
        		
        	}else{
        		Category.registerCategory(name, name.toUpperCase(), "Category with name '" + name + ".", true);
        	}
        }

        sheet.registerElementClassAttributes(Node.class, attributes);
    	sheet.setEditable(false);
    	sheet.setIndentOpaque(false);
    	sheet.setCategoryFontBold(false);
    	sheet.setShowHorizontalLines(false);
    	sheet.setShowVerticalLines(false);
    	sheet.setPropertyExtraIndent(true);
    	sheet.setExpandedIcon(TWaverUtil.getIcon("/demo/sheet/nested/expand.png"));
    	sheet.setCollapsedIcon(TWaverUtil.getIcon("/demo/sheet/nested/collapse.png"));
    	
    	sheet.setElement(node);      

        final JCheckBox opaque = new JCheckBox("Opaque", sheet.isIndentOpaque());
        final JCheckBox bold = new JCheckBox("Bold", sheet.isCategoryFontBold());
        final JCheckBox rowLine = new JCheckBox("RowLine", sheet.getShowHorizontalLines());
        final JCheckBox columnLine = new JCheckBox("ColumnLine", sheet.getShowHorizontalLines());
        
        opaque.setMargin(TWaverConst.NONE_INSETS);
        bold.setMargin(TWaverConst.NONE_INSETS);
        rowLine.setMargin(TWaverConst.NONE_INSETS);
        columnLine.setMargin(TWaverConst.NONE_INSETS);
        alternate.setMargin(TWaverConst.NONE_INSETS);
        
        opaque.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				sheet.setIndentOpaque(opaque.isSelected());
			}
        });
        bold.addChangeListener(new ChangeListener(){
        	public void stateChanged(ChangeEvent e) {
				sheet.setCategoryFontBold(bold.isSelected());
			}
        });
        rowLine.addChangeListener(new ChangeListener(){
        	public void stateChanged(ChangeEvent e) {
				sheet.setShowHorizontalLines(rowLine.isSelected());
			}
        });
        columnLine.addChangeListener(new ChangeListener(){
        	public void stateChanged(ChangeEvent e) {
				sheet.setShowVerticalLines(columnLine.isSelected());
			}
        });
        alternate.addChangeListener(new ChangeListener(){
        	public void stateChanged(ChangeEvent e) {
        		sheet.getCellRendererManager().clearCache();
				sheet.repaint();
			}
        });
        
        TPropertySheetPane sheetPane = new TPropertySheetPane(sheet);
        JToolBar toolbar = sheetPane.getToolbar();
        toolbar.removeAll();
        toolbar.add(sheetPane.getCategoryButton());
        toolbar.add(sheetPane.getSortButton());
        toolbar.add(sheetPane.getDescriptionButton());
        toolbar.addSeparator();
        toolbar.add(alternate);
        toolbar.add(opaque);
        toolbar.add(bold);
        toolbar.add(rowLine);
        toolbar.add(columnLine);
        toolbar.addSeparator();
        toolbar.add(sheetPane.getSearchField());
        this.add(sheetPane);
        
        sheet.addPropertySheetListener(new PropertySheetListener(){
			public void propertySheetChanged(PropertySheetEvent event) {
				if(event.getType() == PropertySheetEvent.ROW_DOUBLE_CLICKED){
					TPropertyDescriptor property = sheet.getPropertyDescriptorByRowIndex(event.getRowIndex());
        			if(property != null){
        				Element element = (Element)property.getElementAttribute().getParam("element");
        				MainPane.box.getSelectionModel().setSelection(element);
        			}
				}
			}
        });

        
	}

}